<?php

include_once F_SOURCE . 'launch/launch.model.class.php';
include_once F_SOURCE . 'ads/ads.model.class.php';

/**
 * 通过关键词搜索文章 操作类
 * 
 * @author Dean
 *
 */
class articleCtrl extends Ctrl{
	
	public function show(){
		$view = new View();
		
		$v = getPostStringParam('v');
		
		$launchModel = new launchModel();
		
		if(!empty($v)){
			$solrUrl = include F_CONF.'solr.config.php';
			$articles = searchArticlesFromSolrByKW($solrUrl['article'], $v);
			foreach ($articles as $key => $article){
				$articleID = $article['id'];
				if(empty($articleID))continue;
				$VBCInfo = $launchModel->findByArticleID($articleID);
				$KWInfo = $launchModel->findKWByArticleID($articleID);
				if(isset($VBCInfo->id))$articles[$key]['byVBC'] = 1;
				if(isset($KWInfo->id))$articles[$key]['byKW'] = 1;
			}
		}else{
			$articles = array();
		}

		$top10 = $this->getTop10ofKws();
		
		$data = array(
				'op_select' => 'article',
				'child_select' => 'show',
				'articleList' => $articles,
				'top10' => $top10,
				'v' => $v,
		);
		
		$view->display($data, 'article-list.html');
	}
	
	private function getTop10ofKws(){
		
		$launchModel  = new launchModel();
		$launchList = $launchModel->findAll(0, 100000);
		
		$countArr = array();	//kws count array
		foreach ($launchList as $launch) {
			$kws = $launch->keywords;
			if(empty($kws))continue;
			$kwArr = explode(',', $kws);
			foreach ($kwArr as $kw){
				if(array_key_exists($kw, $countArr)){
					$countArr[$kw] = intval($countArr[$kw])+1;
				}else{
					$countArr[$kw] = 1;
				}
			}
		}
		
		arsort($countArr, SORT_NUMERIC);
		$countArr = array_keys($countArr);
		
		return array_slice($countArr, 0, 10);
	}
}